package com.example.wnn.web.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.example.wnn.utils.ExportExcelUtils;
import com.example.wnn.utils.ImportExcelUtils;
import com.example.wnn.web.vo.AppUserImportVo;
import com.example.wnn.web.vo.ExportVo;
import com.example.wnn.web.vo.TeacherImportVo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/ieport/")
public class IEportController {

    @PostMapping("export/demo")
    public void exportDemo(HttpServletResponse response){
        List<ExportVo> exportVoList = new ArrayList<>();
        exportVoList.add(new ExportVo("文峥","17773766118"));
        exportVoList.add(new ExportVo("张三","17773766999")) ;
         ExportExcelUtils.export("demo",exportVoList,ExportVo.class,response);
    }

    @PostMapping("import/demo")
    public Object importDemo(@RequestParam("file") MultipartFile file){
      String json = ImportExcelUtils.excelCoverData(file,"", TeacherImportVo.class);
      if (StrUtil.isNotBlank(json)){
          List<TeacherImportVo> teacherImportVos = JSONUtil.toList(JSONUtil.parseArray(json),TeacherImportVo.class);
          return teacherImportVos;
      }
      return null;
    }


    @PostMapping("import/demo2")
    public Object importDemo2(@RequestParam("file") MultipartFile file){
        String json = ImportExcelUtils.excelCoverData(file,"", AppUserImportVo.class);
        Integer size = 0;
        if (cn.hutool.core.util.StrUtil.isNotBlank(json)){
            List<AppUserImportVo> appUserImportVoList = JSONUtil.toList(JSONUtil.parseArray(json),AppUserImportVo.class);
            size = appUserImportVoList.size();
        }
        return String.format("预计导入%s条数据,请等待任务执行完成",size);

    }

}
